package com.hst.web.sys.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.annotation.WebAuth;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.web.auth.po.User;
import com.hst.web.auth.service.UserAuthService;
import com.hst.web.sys.entity.TRole;
import com.hst.web.sys.entity.TUserDept;
import com.hst.web.sys.service.UserService;

/**
 * @author luwenjie
 */
@WebAuth
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    ISQLDao sql;

    @Autowired
    IORDao dao;

    @Autowired
    UserService service;

    @Autowired
    UserAuthService auth;

    @SuppressWarnings({"unchecked"})
    private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> querys = JsonUtil.read(data, Map.class);
        return querys;
    }

    @RequestMapping("/listRoles")
    public void listRoles(Model m) throws JsonGenerationException, JsonMappingException, IOException {
        List<TRole> roles = dao.list(TRole.class);
        m.addAttribute("role", JsonUtil.toString(roles));
    }

    @RequestMapping("/upgrade")
    public void upgrade(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> params = parseQuerys(data);
        String pwd = (String) params.get("pwd");
        String account = (String) params.get("account");
        Encoder encoder = Base64.getEncoder();
        pwd = new String(encoder.encodeToString(pwd.getBytes()));
        User u = (User) auth.getUser(account, pwd); //验证账号密码是否正确
        if (u != null) {// 验证通过升级账号
            String roleId = (String) params.get("roleId");
            String userId = (String) params.get("userId");
            service.upgrade(roleId, userId, m);
        } else { //验证不通过
            m.addAttribute("msg", "error");
        }
    }

    @RequestMapping("/userDept")
    public void userDept(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> params = parseQuerys(data);
        String userId = (String) params.get("userId");
        List<ORQuery> querys = new ArrayList<ORQuery>();
        querys.add(new ORQuery(Op.eq, "userid", userId));
        List<TUserDept> list = dao.list(TUserDept.class, querys);
        m.addAttribute("depts", list);
    }

    @RequestMapping("/allotDept")
    public void allotDept(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> params = parseQuerys(data);
        String userId = (String) params.get("userid");
        String depts = (String) params.get("depts");
        service.allotDept(userId, depts);
        m.addAttribute("msg", "success");
    }

    @RequestMapping("/disable")
    public void disable(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        List<com.hst.web.sys.ui.User> users = JsonUtil.readList(data, com.hst.web.sys.ui.User.class);
        service.disable(users, m);
    }

    @RequestMapping("/enable")
    public void enable(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        List<com.hst.web.sys.ui.User> users = JsonUtil.readList(data, com.hst.web.sys.ui.User.class);
        service.enable(users, m);
    }

    @RequestMapping("/appointmentDisable")
    public void appointmentDisable(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        List<com.hst.web.sys.ui.User> users = JsonUtil.readList(data, com.hst.web.sys.ui.User.class);
        service.appointmentDisable(users, m);
    }

    @RequestMapping("/appointmentEnable")
    public void appointmentEnable(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        List<com.hst.web.sys.ui.User> users = JsonUtil.readList(data, com.hst.web.sys.ui.User.class);
        service.appointmentEnable(users, m);
    }
    
    @RequestMapping("/listRole")
    public void listRole(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        com.hst.web.sys.ui.User user = JsonUtil.read(data, com.hst.web.sys.ui.User.class);
        List<ORQuery> querys = new ArrayList<ORQuery>();
        querys.add(new ORQuery(Op.eq, "roleLevel", user.getUserType()));
        List<TRole> roles = dao.list(TRole.class, querys);
        m.addAttribute("roles", JsonUtil.toString(roles));
    }

    @RequestMapping("/changeRole")
    public void changeRole(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> params = parseQuerys(data);
        String obj = (String) params.get("data");
        String role = (String) params.get("role");
        com.hst.web.sys.ui.User user = JsonUtil.read(obj, com.hst.web.sys.ui.User.class);
        service.changeRole(user, role, m);
    }

    @RequestMapping("/levelRoles")
    public void levelRoles(@RequestBody Map data, Model m) throws JsonParseException, JsonMappingException, IOException {
        if (StringUtils.isNotEmpty((CharSequence) data.get("id")) && data.get("id").equals("comp_admin")) {
            m.addAttribute("role", sql.query("user.rolesLevelsCompAdmin"));
            return;
        }
        m.addAttribute("role", sql.query("user.rolesLevels"));
    }

    @RequestMapping("/employeeUpgrade")
    public void employeeUpgrade(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> params = parseQuerys(data);
        String obj = (String) params.get("employee");
        String account = (String) params.get("account");
        String pwd = (String) params.get("pwd");
        Encoder encoder = Base64.getEncoder();
        String role = (String) params.get("role");
        service.employeeUpgrade(obj, account, role, pwd, m);
    }
}
